//
//  YFMySendedGiftsVC.m
//  project
//
//  Created by yefei on 2019/4/15.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFMySendedGiftsVC.h"
#import "YFMySendedGiftCell.h"
#import "YFMySendedGiftsModel.h"

@interface YFMySendedGiftsVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSArray<YFMySendedGiftModel *> *sendedArray;

@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end

@implementation YFMySendedGiftsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViews];
    [self.tableView.mj_header beginRefreshing];
}

- (void)setupViews {
    self.navigaitonBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupTableView];
}

- (void)setupTableView {
    [self.tableView registerCellByNibName:@"YFMySendedGiftCell"];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.contentInset = UIEdgeInsetsMake(8, 0, 8, 0);
    self.tableView.rowHeight = 102;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.emptyView = self.tipEmptyView;
    
    @weakify(self)
    [self.tableView refresh:^{
        @strongify(self)
        self.page = 1;
        [self loadGiftWithPage:self.page];
    }];
    [self.tableView loadMore:^{
        @strongify(self)
        self.page += 1;
        [self loadGiftWithPage:self.page];
    }];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _sendedArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YFMySendedGiftCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YFMySendedGiftCell" forIndexPath:indexPath];
    if (_sendedArray && indexPath.row < _sendedArray.count) {
        cell.model = _sendedArray[indexPath.row];
    }
    return cell;
}


#pragma mark - 网络请求
- (void)loadGiftWithPage:(NSInteger)page {
    
    BOOL isRefresh = self.tableView.mj_header.state == MJRefreshStateRefreshing;
    
    NSString *postString = [Api stringByAppendingString:MySendedGiftApi];
    NSDictionary *params = @{
                             @"userId": [HJUserManager getUserModel].userId,
                             @"pageCurrent": @(page)
                             };
    @weakify(self)
    [HJHttpTool post:postString params:params success:^(HJResponeds *result) {
        @strongify(self)
        // 获取数据失败
        if (!result.isSuccess) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            if (self.sendedArray.count) {
                // 列表有数据 提示网络信息
                [YFHud showError:result.resultDesc];
            } else {
                self.tableView.mj_footer.hidden = YES;
                self.tipEmptyView.message = result.resultDesc;
                [self.tableView reloadData];
            }
            
            return ;
        }

        // 获取成功
        // 刷新tableView
        NSDictionary *dataDict = result.data;
        YFMySendedGiftsModel *model = [YFMySendedGiftsModel yy_modelWithDictionary:dataDict];
        if (isRefresh) {
            self.sendedArray = model.records;
        } else {
            self.sendedArray = [self.sendedArray arrayByAddingObjectsFromArray:model.records];
        }
        
        if (self.sendedArray.count == 0) {
            self.tipEmptyView.message = @"暂无赠送的礼物～";
            self.tableView.mj_footer.hidden = YES;
        } else {
            self.tableView.mj_footer.hidden = NO;
        }
        
        [self.tableView reloadData];

        // 停止刷新
        if (isRefresh) {
            [self.tableView.mj_header endRefreshing];
        }

        // 是否到底
        BOOL isNoMoreData = model.pages == self.page;

        // 到底了
        if (isNoMoreData) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            // 没有到底
            [self.tableView.mj_footer endRefreshing];
        }
    } failure:^(id result) {
        @strongify(self)
        LKLog(@"%@", result);
        // 停止刷新
        if (isRefresh) {
            [self.tableView.mj_header endRefreshing];
        }
        [self.tableView.mj_footer endRefreshing];
        if (self.sendedArray && self.sendedArray.count) {
            // 列表有数据 提示网络信息
            [YFHud showError:result];
        } else {
            self.tableView.mj_footer.hidden = YES;
            self.tipEmptyView.message = @"网络出问题啦～";
            [self.tableView reloadData];
        }
    }];
}
@end
